/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							 streambuf_operators.cpp

	$Header: /heroes4/streambuf_operators.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "streambuf_operators.h"

#include <algorithm>

#include "debug_switch.h"

// -----------------------------------------------------------------
// t_streambuf_read_failure members
// -----------------------------------------------------------------
char const * t_streambuf_read_failure::what() const throw()
{
	return DEBUG_SWITCH( "Unable to read from std::streambuf.", "" );
}

// -----------------------------------------------------------------
// t_streambuf_write_failure members
// -----------------------------------------------------------------
char const * t_streambuf_write_failure::what() const throw()
{
	return DEBUG_SWITCH( "Unable to write to std::streambuf.", "" );
}

// -----------------------------------------------------------------
// string input function
// -----------------------------------------------------------------
bool read_string16( std::streambuf& buffer, std::string& arg )
{
	t_uint16 size;
	if ( read( buffer, &size, sizeof( size ) ) != sizeof( size ) )
		return false;

	std::string result( size, '\0' );

	std::string::iterator iter = result.begin();
	while ( size > 0 )
	{
		char temp_buf[ 512 ];

		t_uint16 size_to_read = size > sizeof( temp_buf ) ? sizeof( temp_buf ) : size;
		if ( read( buffer, &temp_buf[ 0 ], size_to_read ) != size_to_read )
			return false;
		std::copy( &temp_buf[ 0 ], &temp_buf[ size_to_read ], iter );

		iter += size_to_read;
		size -= size_to_read;
	}

	arg.swap( result );
	return true;
}
